package com.study.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Warehouse;
import com.study.model.pojo.Waretype;
import com.study.services.ProductService;
import com.study.services.WarehouseService;
import com.study.services.WaretypeService;

@RestController
@CrossOrigin
public class WarehouseController {
	@Autowired
	WarehouseService wService;
	@Autowired
	WaretypeService wService2;
	
	/**
	 * 查询所有的仓库信息以及仓库类型
	 * @param warKind
	 * @return
	 */
	@RequestMapping("/findwarehouse")
	public JSONObject findwarehouse(String warKind) {
		JSONObject json=new JSONObject();
		JSONArray json1=new JSONArray();
		JSONArray json2=new JSONArray();
		JSONArray json3=new JSONArray();
		System.out.println("111111");
		//如果传参为空或者全部仓库，则直接查询全部仓库
		if (warKind==null||warKind.length()==0||warKind.equals("查询全部")) {
			List<Warehouse> list=wService.findAll();
			for (Warehouse w : list) {
				JSONObject jo=new JSONObject();
				jo.put("warId", w.getWarId());
				jo.put("warName", w.getWarName());
				jo.put("warAddress", w.getWarAddress());
				jo.put("warArea", w.getWarArea());
				jo.put("warKind", w.getWarKind());
				jo.put("warAdmin", w.getWarAdmin());
				jo.put("warTel", w.getWarTel());
				jo.put("warNote", w.getWarNote());
				jo.put("warStatic", w.getWarStatic());
				json1.add(jo);
			}
		}else {
			List<Warehouse> list3=wService.findByWarKind(warKind);
			for (Warehouse w : list3) {
				JSONObject jo=new JSONObject();
				jo.put("warId", w.getWarId());
				jo.put("warName", w.getWarName());
				jo.put("warAddress", w.getWarAddress());
				jo.put("warArea", w.getWarArea());
				jo.put("warKind", w.getWarKind());
				jo.put("warAdmin", w.getWarAdmin());
				jo.put("warTel", w.getWarTel());
				jo.put("warNote", w.getWarNote());
				jo.put("warStatic", w.getWarStatic());
				json1.add(jo);
			}
		}
		//添加一个全部查询字段,放入库存类型表中
		JSONObject jo1=new JSONObject();
		jo1.put("wtId", "wt000");
		jo1.put("wtName", "查询全部");
		json2.add(jo1);
		List<Waretype> list2=wService2.findAll();
		for (Waretype waretype : list2) {
			JSONObject jo=new JSONObject();
			jo.put("wtId", waretype.getWtId());
			jo.put("wtName", waretype.getWtName());
			json2.add(jo);
		}
		
		json.put("warehouse",json1);
		json.put("waretype", json2);
		return json;
	}
	
	/**
	 * 新增仓库
	 * @param warehouse
	 * @return
	 */
	@RequestMapping("/addwarehouse")
	public JSONObject addwarehouse(Warehouse warehouse) {
		System.out.println("仓库名称： "+warehouse.getWarName());
		System.out.println("仓库名称： "+warehouse.getWarName());
		warehouse.setWarId("war0007");
		warehouse.setWarStatic("使用中");
		wService.addWarehouse(warehouse);
		JSONObject json=new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 修改仓库信息
	 * @param warehouse 修改的仓库信息
	 * @return
	 */
	@RequestMapping("/updatewarehouse")
	public JSONObject updatewarehouse(Warehouse warehouse) {
		JSONObject json=new JSONObject();
		wService.editWarehouse(warehouse);
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 删除仓库
	 * @param warId 仓库编号
	 * @return
	 */
	@RequestMapping("/delwarehouse")
	public JSONObject delwarehouse(String warId) {
		JSONObject json=new JSONObject();
		System.out.println("warId====="+warId);
		wService.delWarehouse(warId);
		json.put("ans", "y");
		return json;
	}
}
